﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Web;

using Joyintech.Common;
using Joyintech.BLL.inpiao365;
using Joyintech.Model.inpiao365;
using Joyintech.Admin.inpiao365;

namespace oyintech.Admin.inpiao365.general.nodeContent
{
    public partial class edit : System.Web.UI.Page
    {
        #region 页面加载

        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.AddUserName.Text = systemUser_BLL.GetInstance().GetCurrentSystemUser().UserName.ToString();
                this.AddDate.Value = DateTime.Now;

                this.LastEditUserName.Text = systemUser_BLL.GetInstance().GetCurrentSystemUser().UserName.ToString();
                this.LastEditDate.Value = DateTime.Now;

                this.LastHitsDate.Value = DateTime.Now;

                //加载常用的“来源网站”
                DataBindSource();

                //加载默认编辑器
                this.litContent.Text = Global.GetTextEditor(this.Content.ID.ToString());

                //绑定栏目
                DataBindsystemNode();

                //判断是否为修改操作
                if (DNTRequest.GetQueryString("Id") != "")
                {
                    DataBindForm();
                }
            }
        }

        #endregion

        #region 绑定栏目

        /// <summary>
        /// 绑定所有栏目
        /// </summary>
        private void DataBindsystemNode()
        {
            this.ddlNodeID.Items.Clear();

            List<systemNode> list = new List<systemNode>();

            list = new systemNode_BLL().GetModelList("ParentID=0" + " order by NodeOrder");

            foreach (systemNode model in list)
            {
                this.ddlNodeID.Items.Add(new ListItem(model.NodeName, model.NodeID.ToString()));

                //递归绑定下级栏目
                DataBindsystemNodeChild(model.NodeID, "--");
            }
        }

        /// <summary>
        /// 递归绑定下级栏目
        /// </summary>
        /// <param name="menuPID"></param>
        private void DataBindsystemNodeChild(int ParentID, string frontStr)
        {
            List<systemNode> list = new List<systemNode>();

            list = new systemNode_BLL().GetModelList("ParentID=" + ParentID + " order by NodeOrder");

            foreach (systemNode model in list)
            {
                this.ddlNodeID.Items.Add(new ListItem(frontStr + model.NodeName, model.NodeID.ToString()));

                //递归绑定下级栏目
                DataBindsystemNodeChild(model.NodeID, "--" + frontStr);
            }
        }

        #endregion

        #region 绑定表单数据

        /// <summary>
        /// 绑定表单数据
        /// </summary>
        private void DataBindForm()
        {
            DataSet ds = new DataSet();

            ds = new systemNodeContent_BLL().GetList("Id=" + DNTRequest.GetQueryString("Id"));

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                Joyintech.WebControls.HFPanel.BindForm(this.HFPanel1, ds.Tables[0].DefaultView);

                this.Content.Value = ds.Tables[0].Rows[0]["txtContent"].ToString();

                this.cbIsTop.Checked = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsTop"].ToString());
                this.cbIsRecommend.Checked = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsRecommend"].ToString());
                this.cbIsHot.Checked = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsHot"].ToString());
                this.cbIsColor.Checked = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsColor"].ToString());
            }
        }

        #endregion

        #region 保存表单数据

        /// <summary>
        /// 保存表单数据到表systemNodeContent
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            systemNodeContent model = new systemNodeContent();

            string strError = string.Empty;

            strError = Joyintech.WebControls.HFPanel.FormCheck(this.HFPanel1, model);

            if (!strError.Equals(""))
            {
                MessageBox.Show(this, strError);
                return;
            }

            Joyintech.WebControls.HFPanel.GetForm(this.HFPanel1, model);

            model.IsTop = this.cbIsTop.Checked;
            model.IsRecommend = this.cbIsRecommend.Checked;
            model.IsColor = this.cbIsColor.Checked;
            model.IsHot = this.cbIsHot.Checked;

            model.TxtContent = this.Content.Value.ToString();

            //操作结果
            int dataResult = 0;

            if (DNTRequest.GetQueryString("Id") != "")
            {
                model.ID = Convert.ToInt32(DNTRequest.GetQueryString("Id").ToString());

                dataResult = new systemNodeContent_BLL().Update(model);
            }
            else
            {
                dataResult = new systemNodeContent_BLL().Add(model);
            }

            if (dataResult >= 1)
            {
                MessageBox.ShowMsgScript(this, "保存成功！", "DG.cancel();ok();");
            }
            else
            {
                MessageBox.Show(this, "保存时发生异常，请重试！");
            }
        }

        #endregion

        #region 绑定常用的几个“来源网站”

        /// <summary>
        /// 绑定常用的几个“来源网站”
        /// </summary>
        private void DataBindSource()
        {
            DataSet ds = new DataSet();

            ds = new systemNodeContent_BLL().GetTopSource(10);

            if(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.ddlSource.DataSource = ds.Tables[0];
                this.ddlSource.DataTextField = "source";
                this.ddlSource.DataValueField = "source";
                this.ddlSource.DataBind();
            }
            this.ddlSource.Items.Insert(0, new ListItem("常用来源", ""));
        }

        #endregion
    }
}